package cm.app.user.service.impl;

import java.util.List;

import net.sourceforge.stripes.integration.spring.SpringBean;
import cm.app.user.form.UserForm;
import cm.app.user.service.UserService;
import cm.data.association.entity.UserRelationship;
import cm.data.association.mapper.UserRelationshipMapper;
import cm.fw.utility.Utilities;

public class UserServiceImpl implements UserService {

    // マップ
    @SpringBean("userRelationshipMapper")
    private UserRelationshipMapper userRelationshipMapper;

    @Override
    public int doCountAll(UserForm form)
            throws Exception {

        // 検索条件を作成
        UserRelationship userRelationship = makeSqlParam(form);
        // 検索
        return userRelationshipMapper.selectCountAllUser(userRelationship);
    }

    @Override
    public List<UserRelationship> doSearch(UserForm form)
            throws Exception {

        // 検索条件を作成
        UserRelationship userRelationship = makeSqlParam(form);
        userRelationship.setLimit(Integer.parseInt(form.getPageCount()));
        userRelationship.setOffset(Integer.parseInt(form.getPageNo())
                * Integer.parseInt(form.getPageCount()));

        return userRelationshipMapper.selectAllUser(userRelationship);

    }

    @Override
    public void delete(UserForm form)
            throws Exception {
        userRelationshipMapper.updateUser(form.getDeleteLoginId());
    }

    private UserRelationship makeSqlParam(UserForm form) {
        // 検索条件を作成
        UserRelationship userRelationship = new UserRelationship();
        if (!Utilities.isNullorEmpty(form.getLoginId()))
            userRelationship.setLoginId(form.getLoginId());
        if (!Utilities.isNullorEmpty(form.getName()))
            userRelationship.setName("%" + form.getName() + "%");
        if (!Utilities.isNullorEmpty(form.getOrganizationId()))
            userRelationship.setOrganizationId(form.getOrganizationId());
        if (!Utilities.isNullorEmpty(form.getOrganizationName()))
            userRelationship.setOrganizationName("%" + form.getOrganizationName() + "%");

        userRelationship.setChkValue(form.getChkValue());

        return userRelationship;
    }

    public UserRelationshipMapper getUserRelationshipMapper() {
        return userRelationshipMapper;
    }

    public void setUserRelationshipMapper(UserRelationshipMapper userRelationshipMapper) {
        this.userRelationshipMapper = userRelationshipMapper;
    }
}
